// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-09-11 09:53:06
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 14:31:14
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Constraint\RRODParser.cpp
 */
#include "Import/BDFImport/BulkData/Constraint/RRODParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Constraint/IRRODService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (RROD);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/RROD/RROD.md>
*/

// 以下需要修改
enum RRODIndex : std::uint8_t
{
    RRODIndex_EID = 1,
    RRODIndex_GA  = 2,  // Grid point identification numbers of connection points. (Integer > 0)
    RRODIndex_GB  = 3,
    RRODIndex_CMA = 4,  // Component number of one and only one dependent translational degree-of-freedom
                        // in the global coordinate system assigned by the user to either GA or GB. (Integer 1,
                        // 2, or 3. Either CMA or CMB must contain the integer, and the other must be blank
                        // for the linear RROD. For Lagrange RROD, both CMA and CMB can be blank.) See Remark  3.
    RRODIndex_CMB   = 5,
    RRODIndex_ALPHA = 6,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseRROD (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::RRODData tempData;

    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;

    size_t judgeRownum = dividedEntryInfo.size () / 10 - 1;
    int    lastindex   = findNonEmptyIndex (dividedEntryInfo, judgeRownum);  // 词条最后一个非空下标
    while (-1 == lastindex)
    {
        judgeRownum--;
        if (judgeRownum < 0)
        {
            throw std::runtime_error ("RBE2Parser: judgeRownum < 0");
        }
        lastindex = findNonEmptyIndex (dividedEntryInfo, judgeRownum);
    }
    // 判断是否存在线膨胀系数
    bool const isFloat = covertToScientificNotation (dividedEntryInfo[lastindex]);
    if (isFloat)
    {
        tempData.m_alpha = convertToReal (dividedEntryInfo[lastindex]);
        lastindex--;  // 去除线膨胀系数
    }

    std::string curStr;
    for (int fieldsColumn = 1; fieldsColumn <= lastindex; fieldsColumn++)
    {
        curStr = dividedEntryInfo[fieldsColumn];

        bool const isEmpty = is_all_space (curStr);
        // 如果当前字段为空白，跳过处理
        if (isEmpty)
        {
            continue;
        }
        switch (fieldsColumn)
        {
        case RRODIndex_EID:
            tempData.m_id = convertToI64 (curStr);
            break;
        case RRODIndex_GA:
            tempData.m_G1 = convertToI64 (curStr);
            break;
        case RRODIndex_GB:
            tempData.m_G2 = convertToI64 (curStr);
            break;
        case RRODIndex_CMA:
            tempData.m_DOF1 = convertToI64 (curStr);
            break;
        case RRODIndex_CMB:
            tempData.m_DOF2 = convertToI64 (curStr);
            break;
        case RRODIndex_ALPHA:
            tempData.m_alpha = convertToReal (curStr);
            break;
        default:
            break;
        }
    }

    return dbServiceFactory->get<IRRODService> ()->Add (tempData);
}
